//
//  HYShortViewController.m
//  KaiJing
//
//  Created by lanou on 16/1/14.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "HYShortViewController.h"
#import "HYShortTableViewCell.h"
#import <MJExtension.h>
#import "PKRequestManager.h"

#import "CJPictorialCellWebViewViewController.h"
#import "HYMainMoviePlagerViewController.h"
@interface HYShortViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView  *tableView;
@property (nonatomic, strong) NSMutableArray *tableArray;


@end
static NSString *identifier = @"HYShortViewController";
@implementation HYShortViewController

//短片
- (void)viewDidLoad {
    [super viewDidLoad];
    _tableArray = [NSMutableArray array];

    self.view.backgroundColor = kRandomClolor;
    [self setUpData];
    
    
    // Do any additional setup after loading the view.
}

- (void) setUpData {
    
    [PKRequestManager requestWithUrlString:@"http://morguo.com/forum.php?mod=threadvideo&androidflag=1&appfrom=ios&iosversion=3.13&page=1" parDic:nil method:GET finish:^(NSDictionary *dataDic) {
        
        NSArray *listArray = dataDic[@"data"][@"list"];
                for (NSDictionary *dic in listArray) {
            _model = [[HYMoreShortModel alloc] init];
            [_model setValuesForKeysWithDictionary:dic];
            [_tableArray addObject:_model];
        }
        
        
        [self addTableView];
        
        
    } error:^(NSError *error) {
        
    }];

}
- (void)addTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 113) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
   // _tableView.rowHeight = 410;
    [_tableView registerClass:[HYShortTableViewCell class] forCellReuseIdentifier:identifier];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HYShortTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.model = _tableArray[indexPath.row];
    
    [cell.shortImageButton addTarget:self action:@selector(shortImageButton:) forControlEvents:(UIControlEventTouchUpInside)];
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat height = [HYShortTableViewCell labelAndCellHeight:_tableArray[indexPath.row]];
    SZLog(@"%f",height);
    return height + kScreenHeight /3 + 120;
    
}

- (void)shortImageButton:(UIButton *)button {
    HYMainMoviePlagerViewController *movieVc = [[HYMainMoviePlagerViewController alloc] init];
    movieVc.movie = _model.videourl;
    [self.navigationController pushViewController:movieVc animated:YES];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CJPictorialCellWebViewViewController *webView = [[CJPictorialCellWebViewViewController alloc] init];
    HYMoreShortModel *model = _tableArray[indexPath.row];
    webView.tid = model.tid;
    [self.navigationController pushViewController:webView animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
